import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

/**
 * @author zhuqiang
 * @version V1.0
 * @date 2015/11/30 22:16
 */
public class Demo {
    /**
     * 计算 分页 参数
     */
    @Test
    public void test() {
        int currentPage = 1;
        int totalPage = 60;

        int firstPage = 1; //第一页
        int lastPage = totalPage; //最后一页
        int start = 0; //开始   2
        int end = 0; //结束     7
        int pre = 0; //前一页   1
        int next = 0; //下一页  2
        int btnNum = 10; //分页按钮个数


        if (totalPage < 2) {
            System.out.println("小于2页，不显示分页");
            return;
        }
        /** 计算开始 **/

        pre = currentPage - 1;
        next = currentPage + 1;

        int mid = btnNum / 2; //中间页
        start = currentPage - mid;
        end = currentPage + mid;

        if (start <= 0) { //如果开始小于等于0，那么就从第二页开始,因为有一个首页占位1
            start = 2;
        }
        if (end >= totalPage) { //如果结束页比总页数大
            end = totalPage;
        }

        if (end - start < btnNum) { //如果按钮个数 不够 指定个数,处理结束往后
            System.out.println("end:temp:" + end);
            end = end + (btnNum - (end - start));  //让结束点 加上 还少的按钮个数
            if (end >= totalPage) { //如果结束点超过了总页数。那么就让结束点 等于总页数
                end = totalPage;
            }
        }

        if(end - start < btnNum){ //如果按钮个数 还是不够。处理开始往前
            System.out.println("end:temp:" + end);
            start = start - (btnNum - (end - start));
            if(start < 2){ //如果太靠前，重置到开始点到2
                start = 2;
            }
        }

        if(next > totalPage){ //下一页 大于 总页数，让下一页=0表示没有；不用处理上一页了，因为当前页等1的时候，-1，刚好等于0
            next = 0;
        }

        StringBuffer sb = new StringBuffer();
        if (pre != 0) {
            sb.append("上一页[" + pre + "];");
        }
        if (firstPage != 0) {
            sb.append("首页[" + firstPage + "];");
        }
        for (int i = start; i < end; i++) {
            sb.append("[" + i + "]");
        }
        if (lastPage != 0) {
            sb.append(";尾页[" + lastPage + "];");
        }
        if (next != 0) {
            sb.append("下一页[" + next + "];");
        }

        System.out.println("------------------\n");
        System.out.println("当前页:" + currentPage + ",限定按钮个数：" + btnNum);
        System.out.println(sb.toString());

        System.out.println(String.format("上一页：%s,首页：%s,start:%s,end:%s,最后一页：%s,下一页：%s", pre, firstPage, start, end, lastPage, next));
    }

    /**
     * 拼接代码 分页代码
     */
    @Test
    public void printBootstrap() {
        int currentPage = 1;
        int totalPage = 60;

        int firstPage = 1; //第一页
        int lastPage = totalPage; //最后一页
        int start = 0; //开始   1
        int end = 0; //结束     5
        int pre = 0; //前一页   1
        int next = 0; //下一页  3
        int btnNum = 10; //分页按钮个数


        if (totalPage < 2) {
            System.out.println("小于2页，不显示分页");
            return;
        }
        /** 计算开始 **/

        pre = currentPage - 1;
        next = currentPage + 1;

        int mid = btnNum / 2; //中间页
        start = currentPage - mid;
        end = currentPage + mid;

        if (start <= 0) { //如果开始小于等于0，那么就从第二页开始
            start = 2;
        }
        if (end >= totalPage) { //如果结束页比总页数大
            end = totalPage;
        }

        if (end - start < btnNum) { //如果按钮个数 不够 指定个数,处理结束往后
            System.out.println("end:temp:" + end);
            end = end + (btnNum - (end - start));  //让结束点 加上 还少的按钮个数
            if (end >= totalPage) { //如果结束点超过了总页数。那么就让结束点 等于总页数
                end = totalPage;
            }
        }

        if(end - start < btnNum){ //如果按钮个数 还是不够。处理开始往前
            System.out.println("end:temp:" + end);
            start = start - (btnNum - (end - start));
            if(start < 2){
                start = 2;
            }
        }

        if(next > totalPage){ //下一页 大于 总页数，那么没有下一页，不用处理上一页了。因为当前页等1的时候，-1，刚好等于0
            next = 0;
        }

        /*
        前一页模版
        <li>
            <a pageno="{0}" href="{1}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        */
        String preTemplate = "<li>\n" +
                "            <a pageno=\"{0}\" href=\"{1}\" aria-label=\"Previous\">\n" +
                "                <span aria-hidden=\"true\">&laquo;</span>\n" +
                "            </a>\n" +
                "        </li>";
        /**
         *  下一页模版
         *  <li>
                <a pageno="{0}" href="{1}" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
         */
        String nextTemplate = "<li>\n" +
                "                <a pageno=\"{0}\" href=\"{1}\" aria-label=\"Next\">\n" +
                "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                "                </a>\n" +
                "            </li>";
        /**
         * 中间页，包首页尾巴页
         * <li><a pageno="{0}" href="{1}">{2}</a></li>
         */
        String midTemplate = "<li><a pageno=\"{0}\" href=\"{1}\">{2}</a></li>";

        /**
         * 中间页，包首页尾巴页
         * <li class="active"><a pageno="{0}" href="{1}">{2}</a></li>
         */
        String currentMidTemplate = "<li class=\"active\"><a pageno=\"{0}\" href=\"{1}\">{2}</a></li>";
        StringBuffer sb = new StringBuffer();
        sb.append("<ul class=\"pagination\">");
        if (pre != 0) {
            sb.append(handerTemplate(preTemplate,pre+"","javascript:void(0);")).append("\n");
        }
        if (firstPage != 0) {
            if(currentPage == firstPage){
                sb.append(handerTemplate(currentMidTemplate,currentPage+"","javascript:void(0);",currentPage+"")).append("\n");
            }else{
                sb.append(handerTemplate(midTemplate,firstPage+"","javascript:void(0);",firstPage+"")).append("\n");
            }
        }
        for (int i = start; i < end; i++) {
            if(currentPage ==  i){
                sb.append(handerTemplate(currentMidTemplate,currentPage+"","javascript:void(0);",currentPage+"")).append("\n");
            }else {
                sb.append(handerTemplate(midTemplate,i+"","javascript:void(0);",i+"")).append("\n");
            }
        }
        if (lastPage != 0) {
            if(currentPage == lastPage){
                sb.append(handerTemplate(currentMidTemplate,currentPage+"","javascript:void(0);",currentPage+"")).append("\n");
            }else {
                sb.append(handerTemplate(midTemplate, lastPage + "", "javascript:void(0);", lastPage + "")).append("\n");
            }
        }
        if (next != 0) {
            sb.append(handerTemplate(nextTemplate,next+"","javascript:void(0);")).append("\n");
        }
        sb.append("</ul>");
        System.out.println("------------------\n");
        System.out.println("当前页:" + currentPage + ",限定按钮个数：" + btnNum);
        System.out.println(sb.toString());

        System.out.println(String.format("上一页：%s,首页：%s,start:%s,end:%s,最后一页：%s,下一页：%s", pre, firstPage, start, end, lastPage, next));
    }

    public String handerTemplate(String template,String... params){
        String result = template;
        for (int i = 0; i < params.length; i++) {
            result = result.replace("{"+i+"}",params[i]);
        }
        return result;
    }

    @Test
    public void random(){
        System.out.println(RandomStringUtils.random(10, false, true));
        System.out.println(RandomStringUtils.random(10, true, true));
        System.out.println(RandomStringUtils.random(10, true, false));
        System.out.println(RandomStringUtils.random(10));
        System.out.println(RandomStringUtils.randomAlphabetic(10));
        System.out.println(RandomStringUtils.randomAscii(10));
        System.out.println(RandomStringUtils.randomNumeric(10));
    }
}
